const api = require('../../utils/api');
const md5 = require('../../utils/md5');

Page({
    data: {
        hasBindStore: false,

        //绑定表单
        form: {
            realName: '',
            phone: '',
            sysUserName: '',
            sysUserPwd: '',
        },

        //已绑定信息
        shopName: '',
        userName: '',
        shopDetail: '', //店铺详情
    },
    onLoad(options) {
        if(options.type) {
            this.getUserInfo();
            this.setData({
                hasBindStore: true
            });
        } else {
            let info = wx.getStorageSync('userinfo');
            this.setData({
                [`form.phone`]: info.phone,
                hasBindStore: false,
            });
        }
    },
    onShow() {

    },

    getUserInfo() { //获取绑定信息
        api.post('/user/info')
            .then(res => {
                this.setData({
                    shopName: res.bindShopName,
                    userName: res.bindUserName,
                });
                this.getStoreInfo(res.bindShopId);
            })
    },
    getStoreInfo(id) { //获取店铺信息
        api.post('/shop/detail?shopId=' + id)
            .then(res => {
                let detail = res;
                //整理营业时间
                let businessBeginTime = detail.businessBeginTime.split(':');
                let businessEndTime = detail.businessEndTime.split(':');
                detail.businessTime = businessBeginTime[0] + ':' + businessBeginTime[1] + '~' + businessEndTime[0] + ':' + businessEndTime[1];
                this.setData({
                    shopDetail: detail
                });
            })
    },

    bindinput(e) { //输入框监听
        let item = e.currentTarget.dataset.item;
        let val = e.detail.value;

        this.setData({
            [item]: val
        });
    },

    validForm() { //表单校验
        if(!this.data.form.realName) {
            wx.showToast({
                title: '请输入您的真实姓名',
                icon: 'none'
            });
            return true;
        }
        if(!this.data.form.phone) {
            wx.showToast({
                title: '请输入您的手机号',
                icon: 'none'
            });
            return true;
        }
        if(this.data.form.phone.length != 11 || this.data.form.phone.indexOf('1') != 0) {
            wx.showToast({
                title: '请检查手机号是否正确',
                icon: 'none'
            });
            return true;
        }
        if(!this.data.form.sysUserName) {
            wx.showToast({
                title: '请输入店铺后台管理员账号',
                icon: 'none'
            });
            return true;
        }
        if(!this.data.form.sysUserPwd) {
            wx.showToast({
                title: '请输入店铺后台管理员登录密码',
                icon: 'none'
            });
            return true;
        }
    },
    submit() {
        if(this.validForm()) {
            return;
        }
        let params = this.data.form;
        params.sysUserPwd = md5.md5(params.sysUserPwd);
        api.post('/user/bind/sysuser', params)
            .then(() => {
                wx.showToast({
                    title: '店铺绑定成功',
                    icon: 'success'
                });
                
                setTimeout(() => {
                    wx.navigateBack(-1);
                }, 1200);
            })
    },
})